﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Main
{
    public partial class CreateLiveID : System.Web.UI.Page
    {
        ControlLiveIDs.Control control = new ControlLiveIDs.Control();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] != null)
            {
                if (Session["LIMITEDUSER"] == null)
                {
                    if (!IsPostBack)
                    {
                        btnDelete.Visible = false;
                        ListExistingLiveIDs();
                    }
                }
                else if (Session["LIMITEDUSER"] == "true")
                {
                    Response.Redirect("Default.aspx");
                }

            }
            else
                Response.Redirect("Login.aspx");
        }

        /// <summary>
        /// triggers listing of existing WindowsLiveIDs on SQL Azure
        /// </summary>
        /// <param name="list"></param>
        private void ListExistingLiveIDs()
        {
            TechnoStoreMainDataContext db = new TechnoStoreMainDataContext();
            var query = from u in db.WindowsLiveIDs
                        orderby u.WL_ID ascending
                        where u.WL_ID != String.Empty && u.Company == Session["Company"].ToString()
                        select u;
 
            foreach (var Result in query)
            {
                if (Result == null)
                {
                    txtExistingLiveID.Text = "No existing Live IDs";
                    btnDelete.Visible = false;
                    break;
                }
                else 
                {
                    txtExistingLiveID.Text = txtExistingLiveID.Text + Result.WL_ID + " ; ";
                    btnDelete.Visible = true;
                }
            }
        }

        /// <summary>
        /// triggers creation of WindowsLiveIDs on SQL Azure
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                
               String msg = control.CreateWindowsLiveId(txtLiveID.Text.Trim().ToLower(),txtLivePW.Text.Trim(),Session["Company"].ToString());

                if (msg == String.Empty)
                {
                    lblError.Text = "Your Windows Live ID has been successfully defined!";
                    ListExistingLiveIDs();
                }
                else
                    lblError.Text = msg;
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
            }
        }

        /// <summary>
        /// triggers deletion of WindowsLiveIDs on SQL Azure
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {

                String msg = control.DeleteWindowsLiveId(Session["Company"].ToString());

                if (msg == String.Empty)
                {
                    lblError.Text = "Existing Windows Live ID has been successfully deleted!";
                    txtExistingLiveID.Text = String.Empty;
                    txtLiveID.Text = String.Empty;
                    ListExistingLiveIDs();
                }
                else
                    lblError.Text = msg;
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
            }
        }
        
        /// <summary>
        /// makes all fields blank
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtLiveID.Text = String.Empty;
            txtLivePW.Text = String.Empty;
            lblError.Text = String.Empty;
        }
    }
}